﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GCTrackableRace.Interfaces;
using log4net;

namespace GcTrackableRace.ScoreCalculator.Helpers
{
	public class GeocacheInfo: IGeocacheInfo
	{
		public GeocacheInfo(IGsakPersistence gsakPersistence, ILog log, IMissingCacheLog missingCacheLog)
		{
			GsakPersistence = gsakPersistence;
			Log = log;
			MissingCacheLog = missingCacheLog;

			GeoCacheCache = new Dictionary<string, IGeocache>();
		}

		private IGsakPersistence GsakPersistence { get; set; }
		private IMissingCacheLog MissingCacheLog { get; set; }
		private ILog Log { get; set; }

		private Dictionary<string, IGeocache> GeoCacheCache { get; set; }


		public IGeocache GetDetail(string gcCode)
		{
			if (!CacheFoundInPersistence(gcCode))
				return null;

			return GeoCacheCache[gcCode];
		}

		#region Private Methods

		private bool CacheFoundInPersistence(string gcCode)
		{
			if (!GeoCacheCache.ContainsKey(gcCode))
			{
				var geocache = GsakPersistence.GetCacheDetails(gcCode);

				if (geocache == null)
				{
					MissingCacheLog.Add(gcCode);
					return false;
				}

				GeoCacheCache.Add(gcCode, geocache);
			}

			return true;
		}

		#endregion Private Mehtods
	}
}